

export class HelpTools {
    public static titleCase(str: string): string {
        let newarr, newarr1 = [];
        newarr = str.toLowerCase().split(" ");
        for (let i = 0; i < newarr.length; i++) {
            newarr1.push(newarr[i][0].toUpperCase() + newarr[i].substring(1));
        }
        return newarr1.join(' ');
    }

    public static randomString(len) {
        len = len || 32;
        let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
        let maxPos = $chars.length;
        let pwd = '';
        for (let i = 0; i < len; i++) {
            pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
    }

    public static GetRandom(maxNum: number, minNum: number): number {
        return Math.floor(Math.random() * (maxNum - minNum + 1) + minNum)
    }

    public static GetRandomScore(maxNum: number, minNum: number): number {
        return Math.floor((Math.random() * (maxNum - minNum + 1) + minNum) * 100) / 100
    }

    /*
　　 * 参数说明：
　　 * @param number：要格式化的数字
　　 * @param  ：保留几位小数
　　 * dec_point：小数点符号
　　 * thousands_sep：千分位符号
　　 * */
    public static number_format(number: any, decimals: number, dec_point?: string, thousands_sep?: string): string {
        number = (number + '').replace(/[^0-9+-Ee.]/g, '');
        let n = !isFinite(+number) ? 0 : +number,
            prec = !isFinite(+decimals) ? 2 : Math.abs(decimals),
            sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
            dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
            s: any = '',
            toFixedFix = function (n, prec) {
                let k = Math.pow(10, prec);
                return '' + Math.ceil(n * k) / k;
            };

        s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
        let re = /(-?\d+)(\d{3})/;
        while (re.test(s[0])) {
            s[0] = s[0].replace(re, "$1" + sep + "$2");
        }

        if ((s[1] || '').length < prec) {
            s[1] = s[1] || '';
            s[1] += new Array(prec - s[1].length + 1).join('0');
        }
        return s.join(dec);
    }

    //保留两位小数, 不足自动补0
    public static returnFloat(val: number): string {
        let value = String(Math.round(parseFloat(String(val)) * 100) / 100);
        let xsd = value.toString().split(".");
        if (xsd.length == 1) {
            value = value.toString() + ".00";
            return value;
        }
        if (xsd.length > 1) {
            if (xsd[1].length < 2) {
                value = value.toString() + "0";
            }
            return value;
        }
    }

    public static UrlSearch(): any {
        let str: any = window.location.href;
        let num = str.indexOf("?");
        if (num > -1) {
            str = str.slice(num + 1);
            str = str.split(/["=","&"]/);
            if (str.length % 2 != 0) {
                str.pop();
            }
        }
        else if (num == -1) {
            //window.location.href = window.location.href + "?index=hall";
            return {};
        }
        let info: any = {}
        for (let i = 0; i < str.length; i += 2) {
            info[str[i]] = str[i + 1];
        }
        return info;
    }
    //{cowType:,}



    /** 
     * 让label的文字滚起来
     *  @param l label组件
     *  @param d 总数
     *  @param delay 执行总时间
     *  @param count 执行总步数
     */
    public static LabelStringRoll(l: cc.Label, d: number, delay?: number, count?: number): void {
        let total = 0;//总数
        let low = 0; //步进
        let i = 0; //步数
        delay = delay == null ? 1000 : delay;
        count = count == null ? 100 : count;
        low = d / count;


        let time = setInterval(() => {
            total += low
            let item = "";
            if (total > 0) {
                item = "+" + total.toFixed(2)
            } else {
                item = total.toFixed(2)
            }
            l.string = item
            i++;
            if (i == count) {
                clearInterval(time)
            }

        }, delay / count)
    }


    ///** 正太分布计算函数 */
    public static randomNormalDistribution(): number[] {
        let u: number = 0, v: number = 0, w: number = 0, c: number = 0;
        do {
            //获得两个（-1,1）的独立随机变量
            u = Math.random() * 2 - 1.0;
            v = Math.random() * 2 - 1.0;
            w = u * u + v * v;
        } while (w == 0.0 || w >= 1.0)

        //这里就是 Box-Muller转换
        c = Math.sqrt((-2 * Math.log(w)) / w);
        //返回2个标准正态分布的随机数，封装进一个数组返回
        //当然，因为这个函数运行较快，也可以扔掉一个
        //return [u*c,v*c];
        return [u * c, v * c];
    }


    //返回花色在素材对应的点数
    public static TileFromID(id: number): string {
        if (id < 0) {
            console.log("非法扑克?")
            return
        }

        if (id < 136) {
            let tmp = id / 4
            let Suit = Math.floor(tmp / 9)
            let Rank = Math.floor(tmp % 9 + 1)
            let Index = Suit * 10 + Rank
            let path = String(Suit) + String(Rank)
            return path
        } else {
            //梅兰竹菊,春夏秋冬
            let cid = 136 + (id - 136) * 4 + 8
            let tmp = cid / 4
            let Suit = Math.floor(tmp / 9)
            let Rank = Math.floor(tmp % 9 + 1)
            let Index = Suit * 10 + Rank
            let path = String(Suit) + String(Rank)
            return path
        }
    }
}